﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace AirLinq.Providers.Sql {
	public class SqlParameter : SqlExpression {
		private readonly object _value;
		private readonly string _name;
		public SqlParameter(string name, object value) {
			if (name == null ){
				throw new ArgumentNullException("name");
			}
			if (value != null
				&& value != DBNull.Value
				&& !(value is string)
				&& !(value is byte[])
				&& !(value is bool)
				&& !(value is DateTime)
				&& !(value is int)
				&& !(value is byte)
				&& !(value is long)
				&& !(value is short)
				&& !(value is decimal)
				&& !(value is double)
				&& !(value is float)) {
				throw new ArgumentException("unsupported type of  'value'");
			}
			_name=name;
			_value=value;
		}
		public DbType DbType {
			get {
				if (_value == null || _value is DBNull || _value is string) {
					return DbType.String;
				}
				if (_value is byte[]) {
					return DbType.Binary;
				}
				if (_value is bool) {
					return DbType.Boolean;
				}
				if (_value is DateTime) {
					return DbType.DateTime;
				}
				if (_value is int) {
					return DbType.Int32;
				}
				if (_value is byte) {
					return DbType.Byte;
				}
				if (_value is long) {
					return DbType.Int64;
				}
				if (_value is short) {
					return DbType.Int16;
				}
				if (_value is decimal) {
					return DbType.Decimal;
				}
				if (_value is double) {
					return DbType.Double;
				}
				if (_value is float) {
					return DbType.Single;
				}
				throw new InvalidOperationException("unsupported type of SqlConstant");				
			}
		}
		public object Value {
			get {
				return _value;
			}
		}
		public override SqlNodeType NodeType {
			get {
				return SqlNodeType.Parameter;
			}
		}
		public string Name {
			get {
				return _name;
			}
		}
	}

}
